/*
 * Copyright (C) 2011 The Code Bakers
 * Authors: Cleuton Sampaio e Francisco Rodrigues
 * e-mail: thecodebakers@gmail.com
 * Project: http://http://code.google.com/p/tcbnewswidget
 * Site: http://www.thecodebakers.org
 *
 * Licensed under the GNU GPL, Version 3.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://gplv3.fsf.org/
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * @author Cleuton Sampaio e Francisco Rogrigues - thecodebakers@gmail.com
 */
package org.thecodebakers.widgets;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.RemoteViews;

public class TCBWidgetProvider extends AppWidgetProvider {

	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager,
			int[] appWidgetIds) {
        Log.d("TCBWidget", "onUpdate()");
        // To prevent any ANR timeouts, we perform the update in a service
        context.startService(new Intent(context, TCBWidgetAtualizador.class));

	}

    public static class TCBWidgetAtualizador extends Service {
        @Override
        public void onStart(Intent intent, int startId) {
    		RemoteViews rviews = new RemoteViews(this.getPackageName(), R.layout.tcbwidgetlayout);
    		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            ComponentName meuWidget = new ComponentName(this, TCBWidgetProvider.class);
            AppWidgetManager manager = AppWidgetManager.getInstance(this);
    		try {
    			rviews.setTextViewText(R.id.txtTitulo, "Updating...");
    			manager.updateAppWidget(meuWidget, rviews);
    			Log.d("TCBWidget", "Beginning update cycle...");
    			DocumentBuilder builder = factory.newDocumentBuilder();
    			Document dom = builder.parse("http://www.thecodebakers.org/feeds/posts/default?alt=rss");
    			Element root = dom.getDocumentElement();
    			NodeList items = root.getElementsByTagName("entry");
    			if (items.getLength() > 0) {
    				Element entrada = (Element) items.item(0);
    				NodeList titulos = entrada.getElementsByTagName("title");
    				Element titulo = (Element) titulos.item(0);
    				String texto = titulo.getFirstChild().getNodeValue();
    				rviews.setTextViewText(R.id.txtTitulo, texto);
    				Log.d("TCBWidget", "Update cycle completed!");
    				manager.updateAppWidget(meuWidget, rviews);
    			}
    		}
    		catch(Exception ex) {
    			Log.d("TCBWidget", ex.getMessage());
    		}
        }

        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }
    }
	
}
